// pages/drink-butler/drink-butler.js
var util = require("../../utils/util.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //图片坐标392,352 为X=0,Y=0 的原点
        equation1: function (x) {
            return   0.0022568269013766643 * x * x  -1.4488828706838186 * x + 342
        },
        equation2: function (x) {
            return   0.002196150956481535 * x * x     -1.5570710281454083 * x  + 220
        },
        waterDropLogList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getWaterList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },
    getWaterList: function () {
        const _this = this
        util.request(getApp().api.butler.getWaterDrop,{type: 0}).then(
            function(res) {
                if (res.code == 200) {
                    let waterDropLogList = res.data.waterDropLogList;

                    if(waterDropLogList.length<6){
                        for(let i=0;i<waterDropLogList.length;i++){
                            let x = 121+ 80*i;
                            console.log(waterDropLogList[i]);
                            waterDropLogList[i]['posX'] = x;
                            waterDropLogList[i]['posY'] = _this.data.equation1(x);
                        }
                    }else{
                        for(let i= 0;i<6;i++){
                            let x = 121+80*i;
                            waterDropLogList[i]['posX'] = x;
                            waterDropLogList[i]['posY'] = _this.data.equation1(x);
                        }

                        for(let i= 6;i<waterDropLogList.length;i++){
                            let x = 19 + (i-6)*80;
                            waterDropLogList[i]['posX'] = x;
                            waterDropLogList[i]['posY'] = _this.data.equation2(x);
                        }
                    }
                    _this.setData({
                        waterList: res.data.list,
                        waterDropLogList:waterDropLogList
                    });
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        //duration: 1500, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                    return
                }
            }
        );
    },
    getTask: function(e) {
        const _this = this
        util.request(getApp().api.butler.insertWaterDrop,{
            waterPoint: e.currentTarget.dataset.id,
            waterNum: e.currentTarget.dataset.num,
        },'post').then(
            function(res) {
                if (res.code == 200) {
                    _this.getWaterList()
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        //duration: 1500, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                    return
                }
            }
        );
    },
    // 更新水滴
    updateWater: function(e) {
        const _this = this
        util.request(getApp().api.butler.updateUserWaterDrop,{
            waterDrop: e.currentTarget.dataset.num
        }).then(
            function(res) {
                if (res.code == 200) {

                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        //duration: 1500, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                    return
                }
            }
        );
    },
    toExchange: function() {
        wx.navigateTo({
            url: '/pages/exchange-list/exchange-list',
        });
    },
    toAchievement: function() {
        wx.navigateTo({
            url: '/pages/achievement/achievement',
        });
    },
    toRule: function() {
        wx.navigateTo({
            url: '/pages/butler-rule/butler-rule',
        });
    },
})